﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using EasyCode.Utility;
using System.Text;
using System.Collections.Generic;
using System.Xml;

namespace EasyCode.Utility
{
    public class Commons
    {
        #region Constructors
        private Commons()
        {
        }
        #endregion

        #region IO
        /// <summary>
        /// Create folder if not exist
        /// </summary>
        /// <param name="folderPath">The folder path</param>
        /// <param name="exception">The ref exception if has durring execute this method</param>
        /// <returns>return true if create folder successfully; otherwise, return false</returns>
        public static bool CreateFolder(string folderPath, ref Exception exception)
        {
            if (!folderPath.EndsWith(@"\"))
                folderPath = String.Format("{0}\\", folderPath);

            string sPath2Check;
            int nPos = folderPath.IndexOf(@"\");
            while (nPos > 0)
            {
                sPath2Check = folderPath.Substring(0, nPos);
                if (!Directory.Exists(sPath2Check))
                {
                    try
                    {
                        Directory.CreateDirectory(sPath2Check);
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                        return false;
                    }
                }

                nPos = folderPath.IndexOf(@"\", nPos + 1);
            }

            return true;
        }

        public static bool DeleteFile(string filePath, ref Exception exception)
        {
            if (!File.Exists(filePath))
                return false;

            try
            {
                File.Delete(filePath);
                return true;
            }
            catch (Exception ex)
            {
                exception = ex;
                return false;
            }
        }

        public static bool IsValidFileOrFolderName(string name)
        {
            name = name.Trim();
            char[] specialChar = new char[] { '\\', '/', ':', '*', '"', '<', '>', '|', '\'' };

            foreach (char chr in specialChar)
            {
                if (name.Contains(chr.ToString()))
                    return false;
            }

            return true;

        }
        #endregion

        #region LinkTitle
        public static string RemoveSign4VietnameseString(string str)
        {
            for (int i = 1; i < VietnameseSigns.Length; i++)
            {
                for (int j = 0; j < VietnameseSigns[i].Length; j++)
                    str = str.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]);
            }
            return str;
        }

        private static readonly string[] VietnameseSigns = new string[]
        {
            "aAeEoOuUiIdDyY",
            "áàạảãâấầậẩẫăắằặẳẵ",
            "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",
            "éèẹẻẽêếềệểễ",
            "ÉÈẸẺẼÊẾỀỆỂỄ",
            "óòọỏõôốồộổỗơớờợởỡ",
            "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",
            "úùụủũưứừựửữ",
            "ÚÙỤỦŨƯỨỪỰỬỮ",
            "íìịỉĩ",
            "ÍÌỊỈĨ",
            "đ",
            "Đ",
            "ýỳỵỷỹ",
            "ÝỲỴỶỸ"
        };
        public static string HandleTitleString(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            str = str.Trim();
            string result = "";
            result = RemoveSign4VietnameseString(HttpUtility.HtmlDecode(str));
            result = result.Replace("`", "");
            result = result.Replace("~", "");
            result = result.Replace("!", "");
            result = result.Replace("@", "");
            result = result.Replace("#", "");
            result = result.Replace("$", "");
            result = result.Replace("%", "");
            result = result.Replace("^", "");
            result = result.Replace("&", "");
            result = result.Replace("*", "");
            result = result.Replace("(", "");
            result = result.Replace(")", "");
            result = result.Replace("-", "");
            result = result.Replace("_", "");
            result = result.Replace("+", "");
            result = result.Replace("=", "");
            result = result.Replace("{", "");
            result = result.Replace("[", "");
            result = result.Replace("}", "");
            result = result.Replace("]", "");
            result = result.Replace("|", "");
            result = result.Replace("\\", "");
            result = result.Replace(":", "");
            result = result.Replace(";", "");
            result = result.Replace("\"", "");
            result = result.Replace("'", "");
            result = result.Replace("<", "");
            result = result.Replace(",", "");
            result = result.Replace(">", "");
            result = result.Replace(".", "");
            result = result.Replace("?", "");
            result = result.Replace("/", "");
            result = result.Replace(" ", "-");
            result = result.Replace("“", "");
            result = result.Replace("”", "");
            result = result.Replace("__", "-");
            return result.ToLower();
        }
        public static string RemoveHtmlTag(string content)
        {
            content = Regex.Replace(content, @"<\s*?[^>]+\s*?>", " ");
            return content.Trim();
        }

        /// <summary>
        /// Cat chuoi
        /// </summary>
        /// <param name="strTitle"></param>
        /// <param name="lim"></param>
        /// <returns></returns>
        public static string LimTitle(object strTitle, int lim)
        {
            string strReturn = string.Empty;
            string tmp = string.Empty;
            if (strTitle != null)
            {
                tmp = strTitle.ToString();
            }
            if (tmp.Length <= lim)
            {
                return tmp;
            }
            for (int i = 0; i < tmp.Length; i++)
            {
                strReturn = strReturn + tmp[i].ToString();
                if (i > lim && tmp[i].ToString().Trim() == string.Empty)
                {
                    break;
                }
            }
            if (strReturn.Length < tmp.Length)
            {
                strReturn = strReturn.Trim() + "...";
            }
            return strReturn;
        }

        public static string SmartLimTitle(object strTitle, int lim)
        {
            string strReturn = string.Empty;
            string tmp = string.Empty;
            bool needSplit = false;
            bool needAdd2 = false;
            if (strTitle != null)
            {
                tmp = strTitle.ToString();
            }
            if (tmp.Length <= lim)
            {
                return tmp;
            }
            for (int i = 0; i < tmp.Length; i++)
            {
                strReturn = strReturn + tmp[i].ToString();
                if (i == lim && tmp[i].ToString().Trim() == string.Empty)
                {
                    break;
                }
                else if (i == lim && tmp[i].ToString().Trim() != string.Empty)
                {
                    needSplit = true;
                    break;
                }
            }
            if (needSplit)
            {
                int lastSpace = strReturn.LastIndexOf(' ');
                if (lastSpace > 0)
                {
                    strReturn = strReturn.Substring(0, lastSpace);
                }
            }
            if (strReturn.IndexOf('(') > 0 && strReturn.IndexOf(')') < 0)
            {
                needAdd2 = true;
                strReturn = strReturn.Trim() + ")";
            }
            if (strReturn.Length < tmp.Length)
            {
                if (needAdd2)
                {
                    strReturn = strReturn.Trim() + "..";
                }
                else
                {
                    strReturn = strReturn.Trim() + "...";
                }
            }
            return strReturn;
        }
        #endregion

        #region Paging

        /// <summary>
        /// Ham nay dung de tao ra cac link cho viec phan trang
        /// </summary>
        /// <param name="url">Dung dan url</param>
        /// <param name="totalRecord">Tong so trang</param>
        /// <param name="recordPerPage">So record tren 1 trnag</param>
        /// <param name="currentPage">Trang hien tai</param>
        /// <param name="type">1:Kieu thuan URL | 2: kieu URL co param</param>
        /// <returns></returns>
        public static string LinkPaging(string url, int totalRecord, int recordPerPage, int currentPage, int type)
        {
            int totalPage = 0;
            if (totalRecord % recordPerPage == 0)
            {
                totalPage = totalRecord / recordPerPage;
            }
            else
            {
                totalPage = totalRecord / recordPerPage + 1;
            }
            if (totalPage > 1)
            {
                StringBuilder pagingLink = new StringBuilder();
                int start = 1;
                int end = 0;
                if (totalPage > 5)
                {
                    if (currentPage > 3)
                    {
                        start = currentPage - 2;
                        if (totalPage - start < 5)
                        {
                            start = totalPage - 4;
                        }
                        if (currentPage + 2 < totalPage)
                        {
                            end = currentPage + 2;
                        }
                        else
                        {
                            end = totalPage;
                        }
                    }
                    else
                    {
                        end = 5;
                    }
                }
                else
                {
                    end = totalPage;
                }
                pagingLink.Append("<div id='paging' class='df_paginator'>");
                string tempUrl = "";
                if (type == 1)
                {
                    tempUrl = url + "?";
                }
                else
                {
                    tempUrl = url + "&";
                }
                if (currentPage > 1)
                {
                    pagingLink.Append("<div class='p_pages'><a href='" + tempUrl + "Page=1'><span>" + "Trang đầu" + "</span></a></div>");
                    pagingLink.Append("<div class='n_pages'><a href='" + tempUrl + "Page=" + (currentPage - 1).ToString() + "'><span>" + "Trước" + "</span></a></div>");
                }
                pagingLink.Append("<div class='listpages'>");
                for (int i = start; i <= end; i++)
                {
                    if (i == currentPage)
                    {
                        pagingLink.Append("<a class='selected'>" + i.ToString() + "</a> ");
                    }
                    else
                    {
                        pagingLink.Append("<a href='" + tempUrl + "Page=" + i.ToString() + "'>" + i.ToString() + "</a> ");
                    }
                }
                pagingLink.Append("</div>");
                if (currentPage + 1 <= totalPage)
                {
                    pagingLink.Append("<div class='n_pages'><a href='" + tempUrl + "Page=" + (currentPage + 1).ToString() + "'><span>" + "Sau" + "</span></a></div>");
                    pagingLink.Append("<div class='p_pages'><a href='" + tempUrl + "Page=" + totalPage.ToString() + "'><span>" + "Trang cuối" + "</span></a></div>");
                }
                pagingLink.Append("</div>");
                return pagingLink.ToString();
            }
            else
            {
                return string.Empty;
            }
        }
        public static string LinkPagingFontEnd(string url, int totalRecord, int recordPerPage, int currentPage, int type)
        {
            int totalPage = 0;
            if (totalRecord % recordPerPage == 0)
            {
                totalPage = totalRecord / recordPerPage;
            }
            else
            {
                totalPage = totalRecord / recordPerPage + 1;
            }
            if (totalPage > 1)
            {
                StringBuilder pagingLink = new StringBuilder();
                int start = 1;
                int end = 0;
                if (totalPage > 5)
                {
                    if (currentPage > 3)
                    {
                        start = currentPage - 2;
                        if (totalPage - start < 5)
                        {
                            start = totalPage - 4;
                        }
                        if (currentPage + 2 < totalPage)
                        {
                            end = currentPage + 2;
                        }
                        else
                        {
                            end = totalPage;
                        }
                    }
                    else
                    {
                        end = 5;
                    }
                }
                else
                {
                    end = totalPage;
                }
                pagingLink.Append("<div class='paging nr'>");
                string tempUrl = "";
                if (type == 1)
                {
                    tempUrl = url + "?";
                }
                else
                {
                    tempUrl = url + "&";
                }
                if (currentPage == 1)
                {
                    pagingLink.Append("<a class='p_first_inactived' href='javascript:void(0);'>First</a>");
                    pagingLink.Append("<a class='p_prev_inactived' href='javascript:void(0);'>Prev</a>");
                }
                else if (currentPage > 1)
                {
                    pagingLink.Append("<a class='p_first' href='" + tempUrl + "Page=1'>First</a>");
                    pagingLink.Append("<a class='p_prev' href='" + tempUrl + "Page=" + (currentPage - 1).ToString() + "'>Prev</a>");
                }

                for (int i = start; i <= end; i++)
                {
                    if (i == currentPage)
                    {
                        pagingLink.Append("<a class='selected'>" + i.ToString() + "</a> ");
                    }
                    else
                    {
                        pagingLink.Append("<a href='" + tempUrl + "Page=" + i.ToString() + "'>" + i.ToString() + "</a> ");
                    }
                }
                if (currentPage + 1 <= totalPage)
                {
                    pagingLink.Append("<a class='p_next' href='" + tempUrl + "Page=" + (currentPage + 1).ToString() + "'>Next</a>");
                    pagingLink.Append("<a class='p_last' href='" + tempUrl + "Page=" + totalPage.ToString() + "'>Last</a>");
                }
                else
                {
                    pagingLink.Append("<a class='p_next_inactived' href='javascript:void(0);'>Next</a>");
                    pagingLink.Append("<a class='p_last_inactived' href='javascript:void(0);'>Last</a>");
                }
                pagingLink.Append("</div>");
                return pagingLink.ToString();
            }
            else
            {
                return string.Empty;
            }
        }
        public static string LinkPagingFrontEnd(string url, int totalRecord, int recordPerPage, int currentPage, int type)
        {
            int totalPage = 0;
            if (totalRecord % recordPerPage == 0)
            {
                totalPage = totalRecord / recordPerPage;
            }
            else
            {
                totalPage = totalRecord / recordPerPage + 1;
            }
            if (totalPage > 1)
            {
                StringBuilder pagingLink = new StringBuilder();
                int start = 1;
                int end = 0;
                if (totalPage > 5)
                {
                    if (currentPage > 3)
                    {
                        start = currentPage - 2;
                        if (totalPage - start < 5)
                        {
                            start = totalPage - 4;
                        }
                        if (currentPage + 2 < totalPage)
                        {
                            end = currentPage + 2;
                        }
                        else
                        {
                            end = totalPage;
                        }
                    }
                    else
                    {
                        end = 5;
                    }
                }
                else
                {
                    end = totalPage;
                }
                pagingLink.Append("<div class='phantrang'><ul>");
                string tempUrl = "";
                if (type == 1)
                {
                    tempUrl = url;// +"?";
                }
                else
                {
                    tempUrl = url;// +"&";
                }
                if (currentPage == 1)
                {
                    pagingLink.Append("<li><a href='javascript:void(0);'>&laquo;</a></li>");
                    pagingLink.Append("<li><a href='javascript:void(0);'>&lsaquo;</a></li>");
                }
                else if (currentPage > 1)
                {
                    pagingLink.Append("<li><a href='" + string.Format(tempUrl, 1) + "'>&laquo;</a></li>");
                    pagingLink.Append("<li><a href='" + string.Format(tempUrl, (currentPage - 1).ToString()) + "'>&lsaquo;</a></li>");
                }

                for (int i = start; i <= end; i++)
                {
                    if (i == currentPage)
                    {
                        pagingLink.Append("<li><a class='chon'>" + i.ToString() + "</a></li>");
                    }
                    else
                    {
                        pagingLink.Append("<li><a href='" + string.Format(tempUrl, i.ToString()) + "'>" + i.ToString() + "</a></li>");
                    }
                }
                if (currentPage + 1 <= totalPage)
                {
                    pagingLink.Append("<li><a href='" + string.Format(tempUrl, (currentPage + 1).ToString()) + "'>&rsaquo;</a></li>");
                    pagingLink.Append("<li><a href='" + string.Format(tempUrl, totalPage.ToString()) + "'>&raquo;</a></li>");
                }
                else
                {
                    pagingLink.Append("<li><a href='javascript:void(0);'>&rsaquo;</a></li>");
                    pagingLink.Append("<li><a href='javascript:void(0);'>&raquo;</a></li>");
                }
                pagingLink.Append("</ul></div>");
                return pagingLink.ToString();
            }
            else
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// Phan trang ma co the doi duoc ten tham so query string page
        /// </summary>
        /// <param name="url"></param>
        /// <param name="totalRecord"></param>
        /// <param name="recordPerPage"></param>
        /// <param name="currentPage"></param>
        /// <param name="type"></param>
        /// <param name="pageArg"></param>
        /// <returns></returns>
        public static string LinkPagingForPageArg(string url, int totalRecord, int recordPerPage, int currentPage, int type, string pageArg)
        {
            int totalPage = 0;
            if (totalRecord % recordPerPage == 0)
            {
                totalPage = totalRecord / recordPerPage;
            }
            else
            {
                totalPage = totalRecord / recordPerPage + 1;
            }
            if (totalPage > 1)
            {
                StringBuilder pagingLink = new StringBuilder();
                int start = 1;
                int end = 0;
                if (totalPage > 5)
                {
                    if (currentPage > 3)
                    {
                        start = currentPage - 2;
                        if (totalPage - start < 5)
                        {
                            start = totalPage - 4;
                        }
                        if (currentPage + 2 < totalPage)
                        {
                            end = currentPage + 2;
                        }
                        else
                        {
                            end = totalPage;
                        }
                    }
                    else
                    {
                        end = 5;
                    }
                }
                else
                {
                    end = totalPage;
                }
                pagingLink.Append("<div class='df_paginator'>");
                string tempUrl = "";
                if (type == 1)
                {
                    tempUrl = url + "?";
                }
                else
                {
                    tempUrl = url + "&";
                }
                if (currentPage > 1)
                {
                    pagingLink.Append("<div class='p_pages'><a href='" + tempUrl + pageArg + "=1'><span>" + "Trang đầu" + "</span></a></div>");
                    pagingLink.Append("<div class='n_pages'><a href='" + tempUrl + pageArg + "=" + (currentPage - 1).ToString() + "'><span>" + "Trước" + "</span></a></div>");
                }
                pagingLink.Append("<div class='listpages'>");
                for (int i = start; i <= end; i++)
                {
                    if (i == currentPage)
                    {
                        pagingLink.Append("<a class='selected'>" + i.ToString() + "</a> ");
                    }
                    else
                    {
                        pagingLink.Append("<a href='" + tempUrl + pageArg + "=" + i.ToString() + "'>" + i.ToString() + "</a> ");
                    }
                }
                pagingLink.Append("</div>");
                if (currentPage + 1 <= totalPage)
                {
                    pagingLink.Append("<div class='n_pages'><a href='" + tempUrl + pageArg + "=" + (currentPage + 1).ToString() + "'><span>" + "Sau" + "</span></a></div>");
                    pagingLink.Append("<div class='p_pages'><a href='" + tempUrl + pageArg + "=" + totalPage.ToString() + "'><span>" + "Trang cuối" + "</span></a></div>");
                }
                pagingLink.Append("</div>");
                return pagingLink.ToString();
            }
            else
            {
                return string.Empty;
            }
        }
        public static string LinkPagingTypeIframe(string url, int totalRecord, int recordPerPage, int currentPage, int type, string align)
        {
            int totalPage = 0;
            if (totalRecord % recordPerPage == 0)
            {
                totalPage = totalRecord / recordPerPage;
            }
            else
            {
                totalPage = totalRecord / recordPerPage + 1;
            }
            if (totalPage > 1)
            {
                StringBuilder pagingLink = new StringBuilder();
                int start = 1;
                int end = 0;
                if (totalPage > 5)
                {
                    if (currentPage > 3)
                    {
                        start = currentPage - 2;
                        if (totalPage - start < 5)
                        {
                            start = totalPage - 4;
                        }
                        if (currentPage + 2 < totalPage)
                        {
                            end = currentPage + 2;
                        }
                        else
                        {
                            end = totalPage;
                        }
                    }
                    else
                    {
                        end = 5;
                    }
                }
                else
                {
                    end = totalPage;
                }
                pagingLink.Append("<div id = 'paging' class='paginator2 " + align + "'>");
                string tempUrl = "";
                if (type == 1)
                {
                    tempUrl = url + "?";
                }
                else
                {
                    tempUrl = url + "&";
                }
                if (currentPage > 1)
                {
                    pagingLink.Append("<a href='" + tempUrl + "Page=1'>" + "&laquo;" + "</a>");
                    pagingLink.Append("<a href='" + tempUrl + "Page=" + (currentPage - 1).ToString() + "'>" + "&lsaquo;" + "</a>");
                }
                for (int i = start; i <= end; i++)
                {
                    if (i == currentPage)
                    {
                        pagingLink.Append("<a class='selected'>" + i.ToString() + "</a> ");
                    }
                    else
                    {
                        pagingLink.Append("<a href='" + tempUrl + "Page=" + i.ToString() + "'>" + i.ToString() + "</a> ");
                    }
                }
                if (currentPage + 1 <= totalPage)
                {
                    pagingLink.Append("<a href='" + tempUrl + "Page=" + (currentPage + 1).ToString() + "'><span>" + "&rsaquo;" + "</span></a>");
                    pagingLink.Append("<a href='" + tempUrl + "Page=" + totalPage.ToString() + "'><span>" + "&raquo;" + "</span></a>");
                }
                pagingLink.Append("</div>");
                return pagingLink.Replace("?", "&").Replace("aspx&", "aspx?").ToString();
            }
            else
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// Ver 2 -- bo qua limitPage va rangePage chỉ can truyen vao totalPage va currentPage
        /// </summary>
        /// <param name="totalPage">Tong so trang</param>
        /// <param name="currentPage">Trang hien tai</param>
        /// <param name="limitPage">Khong can</param>
        /// <param name="rangePage">Khong can</param>
        /// <returns></returns>
        public static string LinkPageForAdmin(int totalPage, int currentPage, int limitPage, int rangePage)
        {
            StringBuilder pagingLink = new StringBuilder();

            if (totalPage > 0)
            {
                if (totalPage <= 3)
                {
                    #region totalPage <= 3
                    for (int i = 1; i <= totalPage; i++)
                    {
                        if (i == currentPage)
                        {
                            pagingLink.Append("<a class=\"selected\" href='javascript:void(0);' onclick=\"loadpage('" + i.ToString() + "')\" >" + i.ToString() + "</a>");
                        }
                        else
                        {
                            pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + i.ToString() + "')\" >" + i.ToString() + "</a>");
                        }
                    }
                    #endregion
                }
                else
                {
                    if (currentPage == 1)
                    {
                        #region currentPage == 1
                        for (int i = 1; i <= 3; i++)
                        {
                            if (i == currentPage)
                            {
                                pagingLink.Append("<a class=\"selected\" href='javascript:void(0);' >" + "1" + "</a>");
                            }
                            else
                            {
                                pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + i.ToString() + "')\" >" + i.ToString() + "</a>");
                            }
                        }

                        pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + (currentPage + 1).ToString() + "')\" >&rsaquo;</a>");
                        pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + totalPage.ToString() + "')\" >&raquo;</a>");
                        #endregion
                    }
                    else if (currentPage == totalPage)
                    {
                        #region currentPage == totalPage
                        pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('1')\" >&laquo;</a>");
                        pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + (totalPage - 1).ToString() + "')\" >&lsaquo;</a>");

                        for (int i = 1; i <= 3; i++)
                        {
                            if (i == rangePage)
                            {
                                pagingLink.Append("<a class=\"selected\" href='javascript:void(0);' >" + totalPage.ToString() + "</a>");
                            }
                            else
                            {
                                pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + (totalPage - 3 + i).ToString() + "')\" >" + (totalPage - 3 + i).ToString() + "</a>");
                            }
                        }
                        #endregion
                    }
                    else
                    {
                        pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('1')\" >&laquo;</a>");
                        pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + (currentPage - 1).ToString() + "')\" >&lsaquo;</a>");
                        pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + (currentPage - 1).ToString() + "')\" >" + (currentPage - 1).ToString() + "</a>");
                        pagingLink.Append("<a class='selected' href='javascript:void(0);' onclick=\"loadpage('" + currentPage.ToString() + "')\" >" + currentPage.ToString() + "</a>");
                        pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + (currentPage + 1).ToString() + "')\" >" + (currentPage + 1).ToString() + "</a>");
                        pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + (currentPage + 1).ToString() + "')\" >&rsaquo;</a>");
                        pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + totalPage.ToString() + "')\" >&raquo;</a>");
                    }
                }
            }
            return pagingLink.ToString();
        }
        public static string LinkPageForAdminNews(int totalPage, int currentPage, int limitPage, int rangePage, int type)
        {
            StringBuilder pagingLink = new StringBuilder();

            if (totalPage <= 3)
            {
                #region totalPage <= 3
                for (int i = 1; i <= totalPage; i++)
                {
                    if (i == currentPage)
                    {
                        pagingLink.Append("<a class=\"selected\" href='javascript:void(0);' onclick=\"loadpage('" + i.ToString() + "','" + type + "')\" >" + i.ToString() + "</a>");
                    }
                    else
                    {
                        pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + i.ToString() + "','" + type + "')\" >" + i.ToString() + "</a>");
                    }
                }
                #endregion
            }
            else
            {
                if (currentPage == 1)
                {
                    #region currentPage == 1
                    for (int i = 1; i <= 3; i++)
                    {
                        if (i == currentPage)
                        {
                            pagingLink.Append("<a class=\"selected\" href='javascript:void(0);' >" + "1" + "</a>");
                        }
                        else
                        {
                            pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + i.ToString() + "','" + type + "')\" >" + i.ToString() + "</a>");
                        }
                    }

                    pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + (currentPage + 1).ToString() + "','" + type + "')\" >&rsaquo;</a>");
                    pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + totalPage.ToString() + "','" + type + "')\" >&raquo;</a>");
                    #endregion
                }
                else if (currentPage == totalPage)
                {
                    #region currentPage == totalPage
                    pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('1','" + type + "')\" >&laquo;</a>");
                    pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + (totalPage - 1).ToString() + "','" + type + "')\" >&lsaquo;</a>");

                    for (int i = 1; i <= 3; i++)
                    {
                        if (i == rangePage)
                        {
                            pagingLink.Append("<a class=\"selected\" href='javascript:void(0);' >" + totalPage.ToString() + "</a>");
                        }
                        else
                        {
                            pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + (totalPage - 3 + i).ToString() + "','" + type + "')\" >" + (totalPage - 3 + i).ToString() + "</a>");
                        }
                    }
                    #endregion
                }
                else
                {
                    pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('1','" + type + "')\" >&laquo;</a>");
                    pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + (currentPage - 1).ToString() + "','" + type + "')\" >&lsaquo;</a>");
                    pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + (currentPage - 1).ToString() + "','" + type + "')\" >" + (currentPage - 1).ToString() + "</a>");
                    pagingLink.Append("<a class='selected' href='javascript:void(0);' onclick=\"loadpage('" + currentPage.ToString() + "','" + type + "')\" >" + currentPage.ToString() + "</a>");
                    pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + (currentPage + 1).ToString() + "','" + type + "')\" >" + (currentPage + 1).ToString() + "</a>");
                    pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + (currentPage + 1).ToString() + "','" + type + "')\" >&rsaquo;</a>");
                    pagingLink.Append("<a href='javascript:void(0);' onclick=\"loadpage('" + totalPage.ToString() + "','" + type + "')\" >&raquo;</a>");
                }
            }

            return pagingLink.ToString();
        }

        //---- Style for mobile cam
        public static string LinkPageAjax(int totalPage, int currentPage, int limitPage, int rangePage)
        {
            StringBuilder pagingLink = new StringBuilder();

            if (totalPage > 0)
            {
                if (totalPage <= 3)
                {
                    #region totalPage <= 3
                    for (int i = 1; i <= totalPage; i++)
                    {
                        if (i == currentPage)
                        {
                            pagingLink.Append("<li><a class=\"rbt_paging_current\" href='javascript:void(0);' onclick=\"loadpage('" + i.ToString() + "')\" >" + i.ToString() + "</a></li>");
                        }
                        else
                        {
                            pagingLink.Append("<li><a class=\"rbt_paging_index\" href='javascript:void(0);' onclick=\"loadpage('" + i.ToString() + "')\" >" + i.ToString() + "</a></li>");
                        }
                    }
                    #endregion
                }
                else
                {
                    if (currentPage == 1)
                    {
                        #region currentPage == 1
                        for (int i = 1; i <= 3; i++)
                        {
                            if (i == currentPage)
                            {
                                pagingLink.Append("<li><a class=\"rbt_paging_current\" href='javascript:void(0);' >" + "1" + "</a></li>");
                            }
                            else
                            {
                                pagingLink.Append("<li><a class=\"rbt_paging_index\" href='javascript:void(0);' onclick=\"loadpage('" + i.ToString() + "')\" >" + i.ToString() + "</a></li>");
                            }
                        }

                        pagingLink.Append("<li><a class=\"rbt_paging_first\" href='javascript:void(0);' onclick=\"loadpage('" + (currentPage + 1).ToString() + "')\" >&rsaquo;</a></li>");
                        pagingLink.Append("<li><a class=\"rbt_paging_first\" href='javascript:void(0);' onclick=\"loadpage('" + totalPage.ToString() + "')\" >&raquo;</a></li>");
                        #endregion
                    }
                    else if (currentPage == totalPage)
                    {
                        #region currentPage == totalPage
                        pagingLink.Append("<li><a class=\"rbt_paging_first\" href='javascript:void(0);' onclick=\"loadpage('1')\" >&laquo;</a></li>");
                        pagingLink.Append("<li><a class=\"rbt_paging_first\" href='javascript:void(0);' onclick=\"loadpage('" + (totalPage - 1).ToString() + "')\" >&lsaquo;</a></li>");

                        for (int i = 1; i <= 3; i++)
                        {
                            if (i == rangePage)
                            {
                                pagingLink.Append("<li><a class=\"rbt_paging_current\" href='javascript:void(0);' >" + totalPage.ToString() + "</a></li>");
                            }
                            else
                            {
                                pagingLink.Append("<li><a class=\"rbt_paging_index\" href='javascript:void(0);' onclick=\"loadpage('" + (totalPage - 3 + i).ToString() + "')\" >" + (totalPage - 3 + i).ToString() + "</a></li>");
                            }
                        }
                        #endregion
                    }
                    else
                    {
                        pagingLink.Append("<li><a class=\"rbt_paging_first\" href='javascript:void(0);' onclick=\"loadpage('1')\" >&laquo;</a></li>");
                        pagingLink.Append("<li><a class=\"rbt_paging_first\" href='javascript:void(0);' onclick=\"loadpage('" + (currentPage - 1).ToString() + "')\" >&lsaquo;</a></li>");
                        pagingLink.Append("<li><a class=\"rbt_paging_index\" href='javascript:void(0);' onclick=\"loadpage('" + (currentPage - 1).ToString() + "')\" >" + (currentPage - 1).ToString() + "</a></li>");
                        pagingLink.Append("<li><a class=\"rbt_paging_current\" class='rbt_paging_current ' href='javascript:void(0);' onclick=\"loadpage('" + currentPage.ToString() + "')\" >" + currentPage.ToString() + "</a></li>");
                        pagingLink.Append("<li><a class=\"rbt_paging_index\" href='javascript:void(0);' onclick=\"loadpage('" + (currentPage + 1).ToString() + "')\" >" + (currentPage + 1).ToString() + "</a></li>");
                        pagingLink.Append("<li><a class=\"rbt_paging_first\" href='javascript:void(0);' onclick=\"loadpage('" + (currentPage + 1).ToString() + "')\" >&rsaquo;</a></li>");
                        pagingLink.Append("<li><a class=\"rbt_paging_first\" href='javascript:void(0);' onclick=\"loadpage('" + totalPage.ToString() + "')\" >&raquo;</a></li>");
                    }
                }
            }
            return pagingLink.ToString();
        }
        public static string LinkPagingMB(string url, int totalPage, int currentPage, int rangePage, int type)
        {
            StringBuilder pagingLink = new StringBuilder();
            string tempUrl = "";
            if (type == 1)
            {
                tempUrl = url + "?";
            }
            else
            {
                tempUrl = url + "&";
            }

            if (totalPage > 0)
            {
                pagingLink.Append("<div class='rbt_paging'><ul>");
                if (totalPage <= 3)
                {
                    #region totalPage <= 3
                    for (int i = 1; i <= totalPage; i++)
                    {
                        if (i == currentPage)
                        {
                            pagingLink.Append("<li><a class=\"rbt_paging_current\"  href=\"" + tempUrl + "page=" + i.ToString() + "\" >" + i.ToString() + "</a></li>");
                        }
                        else
                        {
                            pagingLink.Append("<li><a class=\"rbt_paging_index\"  href=\"" + tempUrl + "page=" + i.ToString() + "\" >" + i.ToString() + "</a></li>");
                        }
                    }
                    #endregion
                }
                else
                {
                    if (currentPage == 1)
                    {
                        #region currentPage == 1
                        for (int i = 1; i <= 3; i++)
                        {
                            if (i == currentPage)
                            {
                                pagingLink.Append("<li><a class=\"rbt_paging_current\"  >" + "1" + "</a></li>");
                            }
                            else
                            {
                                pagingLink.Append("<li><a class=\"rbt_paging_index\"  href=\"" + tempUrl + "page=" + i.ToString() + "\" >" + i.ToString() + "</a></li>");
                            }
                        }

                        pagingLink.Append("<li><a class=\"rbt_paging_next\"  href=\""+tempUrl+"page="+(currentPage+1).ToString()+"\" ></a></li>");
                        pagingLink.Append("<li><a class=\"rbt_paging_last\"  href=\"" + tempUrl + "page=" + totalPage.ToString() + "\" ></a></li>");
                        #endregion
                    }
                    else if (currentPage == totalPage)
                    {
                        #region currentPage == totalPage
                        pagingLink.Append("<li><a class=\"rbt_paging_first\"  href=\"" + tempUrl + "page=1" + "\"></a></li>");
                        pagingLink.Append("<li><a class=\"rbt_paging_prev\"  href=\"" + tempUrl + "page=" + (totalPage - 1).ToString() + "\"></a></li>");

                        for (int i = 1; i <= 3; i++)
                        {
                            if (i == rangePage)
                            {
                                pagingLink.Append("<li><a class=\"rbt_paging_current\">" + totalPage.ToString() + "</a></li>");
                            }
                            else
                            {
                                pagingLink.Append("<li><a class=\"rbt_paging_index\"  href=\"" + tempUrl + "page=" + (totalPage - 3 + i).ToString() + "\" >" + (totalPage - 3 + i).ToString() + "</a></li>");
                            }
                        }
                        #endregion
                    }
                    else
                    {
                        pagingLink.Append("<li><a class=\"rbt_paging_first\"  href=\"" + tempUrl + "page=1" + "\"></a></li>");
                        pagingLink.Append("<li><a class=\"rbt_paging_prev\"  href=\"" + tempUrl + "page=" + (currentPage - 1).ToString() + "\" ></a></li>");
                        pagingLink.Append("<li><a class=\"rbt_paging_index\"  href=\"" + tempUrl + "page=" + (currentPage - 1).ToString() + "\" >" + (currentPage - 1).ToString() + "</a></li>");
                        pagingLink.Append("<li><a class=\"rbt_paging_current\" class='rbt_paging_current '  href=\"" + tempUrl + "page=" + currentPage.ToString() + "\" >" + currentPage.ToString() + "</a></li>");
                        pagingLink.Append("<li><a class=\"rbt_paging_index\"  href=\"" + tempUrl + "page=" + (currentPage + 1).ToString() + "\" >" + (currentPage + 1).ToString() + "</a></li>");
                        pagingLink.Append("<li><a class=\"rbt_paging_next\"  href=\"" + tempUrl + "page=" + (currentPage + 1).ToString() + "\"></a></li>");
                        pagingLink.Append("<li><a class=\"rbt_paging_last\"  href=\"" + tempUrl + "page=" + totalPage.ToString() + "\" ></a></li>");
                    }
                }
                pagingLink.Append("</ul></div>");
            }
            return pagingLink.ToString();
        }

        #endregion Paging

        #region Capcha
        public static string CaptCha
        {
            get
            {
                if (HttpContext.Current.Session["captchar"] == null)
                    HttpContext.Current.Session.Add("captchar", string.Empty);

                return Protector.String(HttpContext.Current.Session["captchar"]);
            }
            set
            {
                HttpContext.Current.Session["captchar"] = value.Trim();
            }
        }

        #endregion Capcha

        #region MD5
        public static string MD5(string md5string)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(md5string);
            bs = x.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            return s.ToString();
        }
        #endregion

        #region Img Upload
        public static string[] strImage = { ".GIF", ".PNG", ".JPG", ".DOC", ".RAR", ".ZIP", ".PDF" };
        public static string[] ARRAY_IMAGE_TYPE = { ".GIF", ".PNG", ".JPG", ".JPEG" };

        public static bool CheckImageType(string strFileType)
        {
            bool isValid = false;
            if (string.IsNullOrEmpty(strFileType))
            {
                isValid = false;
            }
            else
            {
                strFileType = strFileType.ToUpper();
                for (int i = 0; i < strImage.Length; i++)
                {
                    if (strImage[i].ToString().ToUpper() == strFileType)
                    {
                        isValid = true;
                        break;
                    }
                }
            }
            return isValid;
        }

        public static bool CheckImageWidth(FileUpload fileUpload)
        {
            //System.Drawing.Image myImage = System.Drawing.Image.FromStream(fileUpload.PostedFile.InputStream);
            //if (myImage.Width > SystemConfig.GetImageMaxWidth)
            //{
            //    return false;
            //}
            return true;
        }

        public static string[] strMovie = { ".AVI", ".FLV", ".SWF" };
        public static bool CheckMovieType(string strFileType)
        {
            bool isValid = false;
            if (string.IsNullOrEmpty(strFileType))
            {
                isValid = false;
            }
            else
            {
                strFileType = strFileType.ToUpper();
                for (int i = 0; i < strMovie.Length; i++)
                {
                    if (strMovie[i].ToString() == strFileType)
                    {
                        isValid = true;
                        break;
                    }
                }
            }
            return isValid;
        }

        public static string GetDefaultImageURL(int size)
        {

            //string result = string.Empty;
            //if (HttpContext.Current != null)
            //{
            //    System.Web.UI.Page p = HttpContext.Current.Handler as System.Web.UI.Page;
            //    if (p != null)
            //    {
            //        if (size == 1)
            //        {
            //            result = p.ResolveUrl(SystemConfig.GetImagePathDefault + Constants.DEFAULT_IMAGE_SMALL);
            //        }
            //        else if (size == 2)
            //        {
            //            result = p.ResolveUrl(SystemConfig.GetImagePathDefault + Constants.DEFAULT_IMAGE_MEDIUM);
            //        }
            //        else if (size == 3)
            //        {
            //            result = p.ResolveUrl(SystemConfig.GetImagePathDefault + Constants.DEFAULT_IMAGE_LARGE);
            //        }
            //        else
            //        {
            //            result = p.ResolveUrl(SystemConfig.GetImagePathDefault + Constants.DEFAULT_IMAGE_SMALL);
            //        }
            //        result = string.Format("this.onerror=null;this.src=\"{0}\"", result);
            //    }
            //}

            //return result;
            return string.Empty;
        }

        public static bool CheckValidFileType(FileUpload fileUpload)
        {
            string fileName = HttpContext.Current.Server.HtmlEncode(fileUpload.FileName);
            string extension = System.IO.Path.GetExtension(fileName);
            return CheckImageType(extension);
        }

        public static bool UploadFile(FileUpload fileUpload, ref string fileName, string fileFolder, ref string error, string[] fileType, int? width, int? height)
        {
            strImage = fileType;
            if (CheckValidFileType(fileUpload))
            {
                if (width != null && height != null)
                {
                    System.Drawing.Image myImage = System.Drawing.Image.FromStream(fileUpload.PostedFile.InputStream);
                    if (width != myImage.Width || height != myImage.Height)
                    {
                        error="";
                        return false;
                    }
                }

                string filePath = string.Empty;
                try
                {
                    string extension = Path.GetExtension(fileUpload.FileName);
                    fileName = Guid.NewGuid().ToString() + extension;
                    filePath = HttpContext.Current.Server.MapPath(fileFolder + fileName);
                    fileUpload.SaveAs(filePath);

                }
                catch (Exception ex)
                {
                    error = ex.ToString() + filePath;
                    return false;
                }
                return true;
            }
            else
            {
                string types = "";
                for (int i = 0; i < fileType.Length; i++)
                {
                    types = types + fileType[i] + ", ";
                }
                types = types.Remove(types.Trim().Length - 1, 1);
                error="";// string.Format(MessageConst.INVALID_FILE_TYPE, types);
                return false;
            }
        }
        public static bool Upload(FileUpload file, string folder, string[] fileType, ref string returnFilePath, ref string error)
        {
            strImage = fileType;
            if (CheckValidFileType(file))
            {
                try
                {
                    string extension = Path.GetExtension(file.FileName);
                    string fileName = Guid.NewGuid().ToString() + extension;
                    string filePath = HttpContext.Current.Server.MapPath(folder + fileName);
                    file.SaveAs(filePath);
                    returnFilePath = folder + fileName;
                    return true;
                }
                catch (Exception ex)
                {
                    error = ex.Message;
                    return false;
                }
            }
            error = "Invalid file type!";
            return false;
        }
        #endregion
      
    }
}
